<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Search;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

use App\Models\Search\SearchResult;

class SearchController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Search(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->fixColumns(3, -3);
            $grid->column('id')->sortable();
            $grid->column('uid')->sortable();
            $grid->column('user_identity', '昵称')->display(function () {
                return isset($this->user->nickname) ? $this->user->nickname : "-";
            });
            $grid->column('sex', '性别');
            $grid->column('city');
            $grid->column('age', '搜索年龄');
            $grid->column('height', '搜索身高')->display(function () {
                return "{$this->height} CM";
            });
            $grid->column('face_score', '颜值标签');
            $grid->column('college_tab', '学院标签');
            $grid->column('family', '家庭标签');
            $grid->column('income', '收入标签');
            $grid->column('constellation', '星座');
            $grid->column('carandhouse', '车房');
            $grid->column('keyword')->explode(',')->label();
            // $grid->column('pay_id', '支付记录编号');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->column('status')->using(['0' => '未支付', '1' => '已支付', '2' => '已完成'])->label([
                'default' => 'primary',
                '0' => 'danger',
                1 => 'primary',
                2 => 'success',
            ]);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('uid', '会员id');
                $filter->like('user.nickname', '会员昵称');
            });
            $grid->export();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Search(), function (Show $show) {
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('id');
                $show->width(4)->field('uid');
                $show->width(4)->field('sex','性别');
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('age', '年龄');
                $show->width(4)->field('height', '身高');
                $show->width(4)->field('face_score', '颜值');
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('college_tab', '学校标签');
                $show->width(4)->field('family', '家庭情况');
                $show->width(4)->field('income', '收入');
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('constellation', '星座');
                $show->width(4)->field('carandhouse', '车房');
                $show->width(4)->field('keyword', '关键词');
            });
            $show->row(function (Show\Row $show) {
                $show->width(4)->field('city');
            });
            $show->relation('搜索结果记录', function ($model) {
                $grid = new Grid(new SearchResult);
                $grid->model()->where('search_id', $model->id);
                $grid->id()->width("10%");
                $grid->column('result_id', '结果id')->width("10%");
                $grid->column('content', '结果内容')->width("70%")->display(function () {
                    if ($this->type == 'i') {
                        $content = "{$this->idata->users->nickname}  {$this->idata->sex} {$this->idata->birthday} {$this->idata->face_score} {$this->idata->constellation} {$this->idata->height} {$this->idata->city} {$this->idata->education} {$this->idata->college} {$this->idata->college_tab} {$this->idata->income} {$this->idata->marriage} {$this->idata->car_and_house}";
                    } else {
//                        $content = $this->edata->content;
                        $data = json_decode($this->edata->content, true);
                        $sex = '无';
                        if (isset($data['sex'])) {
                            $sex = $data['sex'] == 1 ? '男' : '女';
                        }
                        $nickname = "";
                        if (isset($data['nickname'])) {
                            $nickname = $data['nickname'];
                        }
                        $marriage = "";
                        if (isset($data['marriage'])) {
                            $marriage = $data['marriage'];
                        }
                        $content = "{$nickname} {$sex} {$data['age']} {$data['constellation']} {$data['height']} {$data['location']['city']} {$data['education']} {$data['school_type']} {$data['annual_income']} {$data['marital_status']} {$marriage} {$data['assets']}";
                    }
                    return $content;
                });
                $grid->disableRefreshButton();
                $grid->disableFilterButton();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->disableActions();
                return $grid;
            });

            $show->panel()->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Search(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('sex');
            $form->text('city');
            $form->text('min_age');
            $form->text('max_age');
            $form->text('min_height');
            $form->text('max_height');
            $form->text('min_education');
            $form->text('max_education');
            $form->text('keyword');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
